*cd "C:\Users\eyurdagu\Dropbox\minW_insurance\codes\outputs"


insheet parameters using "load_pars.txt", clear 

gen nothing=1
gen ord=_n
reshape wide parameters, i(nothing) j(ord)

rename parameters1 gamma
rename parameters3 alpha
rename parameters4 eta
rename parameters6 rho
rename parameters11 pselect
*rename parameters20 benefit

keep gamma alpha eta pselect rho
save "params.dta", replace


insheet benefit using "b_unemp.txt", clear 
merge using params.dta
drop _merge
save "params.dta", replace



*cd "store/rho_1"
insheet a z tau x wage unemp labor labori ki yi employed distw distfirm choiceprob apr switchz mu changefirm vsum vinit minw xgrid using "pol.txt" , comma clear

destring, replace force

append using "params.dta"
foreach x in gamma alpha eta pselect benefit rho {
sum `x'
replace `x'=r(mean) if `x'==.
}


*merge 1:1 a z x using "pol_values.dta"

gen val=pselect*vsum+(1-pselect)*vinit

destring wage, replace force

gen weightinemployed=distw*(1-unemp)
gen weight=distw

sum unemp [aw=weight]
gen unempall=r(mean)

gen wagewinsor=wage

sum wage [aw=weightinemployed], d
replace wagewinsor=r(p99) if wage>r(p99)

sum wage [aw=weightinemployed], d
replace wagewinsor=r(p1) if wage<r(p1)

xtile decilez=z [aw=distfirm], nquantiles(10)

*gen changefirm=1-stayfirm
*cd "store/rho03"

egen iz=group(z)
egen itau=group(tau)
egen iztau=group(z tau)
*egen ix=group(x)
egen ia=group(a)

rename xgrid ix

foreach x in wage x wagewinsor z employed yi {
gen log`x'=log(`x')
}

sum logwagewinsor [aw=weightinemployed]
gen sdw=r(sd)

sum logwagewinsor [aw=weightinemployed], d
gen skewwage=r(skewness)
gen kurtwage=r(kurtosis)


foreach xx in z employed yi {
foreach i in 10 25 50 {
sum `xx' [aw=distfirm], d
sum wagewinsor [aw=weightinemployed] if `xx'>r(p`=100-`i'')
gen wagetop`xx'`i'=r(mean)

sum `xx' [aw=distfirm], d
sum wagewinsor [aw=weightinemployed] if `xx'<=r(p`i')
gen wagebottom`xx'`i'=r(mean)

}
}

foreach xx in z employed yi {
foreach i in 10 25 50 {
gen topbottom`xx'`i'gap=wagetop`xx'`i'/wagebottom`xx'`i'
}
}

gen minwclose=(wagewinsor<=1.1*minw)
sum minwclose [aw=weightinemployed]
gen minwcloserate=r(mean)

sum wage [aw=weightinemployed]
gen replacement=benefit/r(mean)

sum wage [aw=weightinemployed]
gen minwmeanwage=minw/r(mean)

sum switchz [aw=weightinemployed]
gen meanswitchz=r(mean)

sum changefirm [aw=weightinemployed]
gen meanchangefirm=r(mean)


gen logwox=log(wage/x^rho)
gen logxrho=log(x^rho)

gen sdwoxinfirm=.
gen sdxrhoinfirm=.
sum iztau
local ilast=r(max)
forvalues i=1/`ilast' {
sum logwox if iztau==`i' [aw=weightinemployed]
replace sdwoxinfirm=r(sd) if iztau==`i'

sum logxrho if iztau==`i' [aw=weightinemployed]
replace sdxrhoinfirm=r(sd) if iztau==`i'
}

foreach ii in woxinfirm xrhoinfirm {
sum sd`ii' [aw=weightinemployed]
gen avgsd`ii'=r(mean)

sum sd`ii' [aw=distfirm]
gen avgsd`ii'unw=r(mean)
}

sum logwox [aw=weightinemployed]
gen sdwoxoverall=r(sd)

sum a [aw=distw], d
foreach i in 10 25 75 90 99 {
gen assetsp`i'p50=r(p`i')/r(p50)
}

sum a [aw=distw], d
gen sharea=a/r(sum)

xtile apctile=a [aw=distw], nquantiles(100)


sum sharea [aw=distw] if apctile>=99, d
gen sharea99=r(sum)

sum sharea [aw=distw] if apctile>=90, d
gen sharea90=r(sum)

sum sharea [aw=distw] if apctile>=75, d
gen sharea75=r(sum)

sum sharea [aw=distw] if apctile>=50, d
gen sharea50=r(sum)

*replace distwtemp=int(distw*10000000000000)
*ginidesc a [aw=distwtemp], by(ix) 



foreach ii in logz logemp logyi {
sum `ii' [aw=distfirm], d
gen sd`ii' =r(sd)
gen kurt`ii' =r(kurtosis)
gen skew`ii' =r(skewness)
}

gen sdwagewithinfirm=.
gen bitefirm=.
gen avgwagefirm=.
gen avglogwagefirm=.

sum iztau
local ilast=r(max)
forvalues i=1/`ilast' {
sum logwage if iztau==`i'  [aw=weightinemployed]
replace sdwagewithinfirm=r(sd) if iztau==`i'

sum minwclose if iztau==`i' [aw=weightinemployed]
replace bitefirm=r(mean) if iztau==`i'

sum wage if iztau==`i' [aw=weightinemployed]
replace avgwagefirm=r(mean) if iztau==`i'

sum logwage if iztau==`i' [aw=weightinemployed]
replace avglogwagefirm=r(mean) if iztau==`i'
}

sum sdwagewithinfirm  [aw=weightinemployed]
gen avgsdwagewithinfirm=r(mean)

sum sdwagewithinfirm if ix==1 & ia==1 [aw=distfirm]
gen avgsdwagewithinfirmunw=r(mean)

*by z, sort : egen float sumdistw = sum(distw)
*gen temp=sumdistw/distfirm

by z tau x, sort : egen float employedx = sum(weightinemployed)
replace employedx=employedx/distfirm

gen sharexinfirm=employedx/employed

gen sdsharexinfirm=.
sum iztau
local ilast=r(max)
forvalues i=1/`ilast' {
sum sharexinfirm if iztau==`i'
replace sdsharexinfirm=r(sd) if iztau==`i'
}

gen sdsharexinx=.
sum ix
local ilast=r(max)
forvalues i=1/`ilast' {
sum sharexinfirm if ix==`i'
replace sdsharexinx=r(sd) if ix==`i'
}

sum sdsharexinfirm  [aw=weightinemployed]
gen avgsdsharexinfirm=r(mean)

sum sdsharexinfirm if ix==1 & ia==1 [aw=distfirm]
gen avgsdsharexinfirmunw=r(mean)


sum sdsharexinx  [aw=weightinemployed]
gen avgsdsharexinx=r(mean)

sum sdsharexinx if iz==1 & ia==1 [aw=distfirm]
gen avgsdsharexinxunw=r(mean)

sum bitefirm if ix==1 & ia==1 [aw=distfirm]
gen avgbitefirm=r(mean)


sum bitefirm if ix==1 & ia==1 [aw=distfirm]
gen sdbitefirm=r(sd)


gen logavgwagefirm=log(avgwagefirm)
sum logavgwagefirm if ix==1 & ia==1 [aw=distfirm]
gen sdlogavgwagefirm=r(sd)

sum avglogwagefirm if ix==1 & ia==1 [aw=distfirm]
gen sdavglogwagefirm=r(sd)

save "pol.dta", replace

preserve

collapse (mean) topbottomz10 topbottomz25 topbottomz50 topbottomemployed10 topbottomemployed25 topbottomemployed50 topbottomyi10 topbottomyi25 topbottomyi50 ///
 sdw skewwage kurtwage minwcloserate minwmeanwage replacement unempall meanswitchz meanchangefirm sdlogz kurtlogz skewlogz  ///
 sdlogemp kurtlogemp skewlogemp sdlogyi kurtlogyi skewlogyi avgsdwagewithinfirm avgsdwagewithinfirmunw avgsdsharexinfirm avgsdsharexinfirmunw ///
 avgsdsharexinx avgsdsharexinxunw avgbitefirm sdbitefirm sdlogavgwagefirm sdavglogwagefirm avgsdwoxinfirm avgsdwoxinfirmunw sdwoxoverall avgsdxrhoinfirm avgsdxrhoinfirmunw ///
 assetsp10p50 assetsp25p50 assetsp75p50 assetsp90p50 assetsp99p50 sharea99 sharea90 sharea75 sharea50
 
save "moments.dta", replace
xpose, clear varname 

outsheet _varname v1  using "moments.txt", comma replace
*outsheet v1  using "moments_nonames.txt", comma replace nonames
restore


preserve
collapse (mean) bitefirm wage tau z logz unemp [aw=distfirm], by(itau iz)

gen logtau=log(tau) 
gen logwage=log(wage)

replace logwage=5 if logwage<=5

twoway contour bitefirm tau z, ztitle("Minimum wage bite", size(large)) ///
 ylabel(, nogrid) ytitle("{&tau}", size(large)) xtitle("z", size(large))  ///
 graphregion(fcolor(white) lcolor(white)) plotregion(margin(large)) yscale(range(-0.2 0.2)) ylabel(-0.2(0.1)0.2) xlabel(#5) ///
 minmax interp(none)  ecolor(red) crule(chue) scolor(green) 
graph export bitefirmheatmap.pdf, replace

twoway contour logwage tau z, zlabel(5(0.2)5.8)   ztitle("Avg. log-wage", size(large)) ///
 ylabel(, nogrid) ytitle("{&tau}", size(large)) xtitle("z", size(large))  ///
 graphregion(fcolor(white) lcolor(white)) plotregion(margin(large)) yscale(range(-0.2 0.2)) ylabel(-0.2(0.1)0.2) xlabel(#5) ///
 interp(none)  ecolor(red) crule(chue) scolor(green) 
graph export wageheatmap.pdf, replace

/*
twoway contour unemp tau z, ztitle("Unemp. rate", size(large)) ///
 ylabel(, nogrid) ytitle("{&tau}", size(large)) xtitle("z", size(large))  ///
 graphregion(fcolor(white) lcolor(white)) plotregion(margin(large)) yscale(range(-0.2 0.2)) ylabel(-0.2(0.1)0.2) xlabel(#5) ///
 minmax interp(none)  ecolor(red) crule(chue) scolor(green) 
graph export uheatmap.pdf, replace
*/

restore



